var timer;
var firstMM0 = true;
var auth;

$(function(){
	console.info("注入5166000.js成功！！！");

    auth = $.parseJSON(localStorage.getItem("auth"));
    /**
     * 向也没注入获取历史记录和获取时间的按钮
     */
    $(".time").prepend("<div style='height:45px;'><input type='button' value='开始' id='get-history-btn'/> <input type='button' value='一键购买大' id='get-time-btn'/></div>");

    /**
     * 绑定点击获取时间按钮
     */
    $("body").on("click","#get-time-btn",function(){
        let no = parseInt($(".history .prompt").text().substring(1,12))+1;//获取开奖号码
        goumai(no,"大")
        // pdkj();
        // console.info(auth.value.token)
        // getPcList(parseInt("20180927025"),"大");

    });

    /**
     * 绑定点击获取历史记录按钮
     */
    $("body").on("click","#get-history-btn",function(){
        console.info("开始检测倒计时")
        timer = setInterval("checkTime()", 10000);//每10秒检测一次页面倒计时剩余多少
    });

})



/**
 * 检查倒计时还剩多长时间
 */
function checkTime() {
    //倒计时分钟小于1那么就去获取上一期的开奖号码//计算大小
    let mm = $(".time span:eq(1)").text();
    let ss = $(".time span:eq(2)").text();

    if (mm == 0){//分钟数小于0那么就去获取开奖信息
        if (firstMM0) {
            console.info("倒计时低于1分钟,并且是第一次检测到0，去检测上一期开奖号码",mm,ss)
            pdkj();
            // clearInterval(timer);//倒计时检测暂时停止
            firstMM0 = false;
        }else {
            console.info("不是第一次检测到低于1分钟，不去执行获取期号操作")
        }

    }else {
        firstMM0 = true;//第一次检测到0初始化
        console.info("倒计时大于1分钟，重复检测。。。",mm,ss)
    }

    //去历史记录查询对应的期数购买的大小

    //中奖了就去购买

}

/**
 * 判断开奖方法
 */
function pdkj() {
    let kjhmDiv = $(".history .number div");//获取开奖号码
    let kjhm ="";
    let dx;
    let he = 0;
    $.each(kjhmDiv,function (i,d) {
        he=he+parseInt($(d).text());
        kjhm=kjhm+$(d).text();
    })

    if (45 >= he && he >= 23){
        dx="大"
    }else {
        dx="小";
    }

    // console.info(kjhm,dx)
    let no = $(".history .prompt").text().substring(1,12);//获取开奖号码
    console.info("要检测的开奖期号",no,kjhm,dx);
    getPcList(no,dx);
}

/**
 * 购买大小
 */
function goumai(issueNo,content) {
    $.ajax({
        headers: {
            "Auth-Identity": "normal",
            "Auth-Token": auth.value.token,
            "Referer": "https://5166000.com/memberCenter",
            "Sign": "c2ca411d9e02644d2e15b38bc26c95e6"
        },
        type: 'post', //HTTP请求类型
        url:'https://5166000.com/api_digital/Bet/bet' ,
        dataType: 'json',
        data:getGMData(issueNo,content),
        success: function(data, status, xhr) {
            console.info("购买完毕查看购买结果",data)
        },
        error: function(xhr, type, errorThrown) {
            console.info("异常："+errorThrown);
        }
    });

}

/**
 * 获取购买的发送数据
 * @param issueNo 要购买的期数
 * @param content 要购买的大小
 * @returns {{issueNo: *, lotteryId: number, nonce: string, orderList: [*], planList: [*,*,*,*,*], timestamp: number, winStop: number}}
 */
function getGMData(issueNo,content) {
    return {
        issueNo: issueNo,
        lotteryId: 1,
        nonce: "d2584ddf-64a1-4a9d-a1cb-3bfce4dbbd4e",
        orderList: [
            {
                content: content,
                method_name: "daxiaodanshuang_zonghe",
                mode: 0.01,
                playId: 2014,
                position: "",
                realName: "总和",
                rebatePoint: "0",
                stakeCount: 1,
                totalPrice: 0.02,
                unitPrice: 2
            }
        ],
        planList: [
            {
                issueNo: issueNo,
                multiple: 2,
                selected: true
            },
            {
                issueNo: issueNo+1,
                multiple: 2,
                selected: true
            },
            {
                issueNo: issueNo+2,
                multiple: 2,
                selected: true
            },
            {
                issueNo: issueNo+3,
                multiple: 2,
                selected: true
            },
            {
                issueNo: issueNo+4,
                multiple: 2,
                selected: true
            }
        ],
        timestamp: 1537884222,
        winStop: 1
    }
}


/**
 * 模拟获取历史购买记录
 */
function getPcList(issueNo,dx){
    console.info("获取购买的历史记录")
    $.ajax({
        headers: {
            "Auth-Identity": "normal",
            "Auth-Token": auth.value.token,
            "Sign": "c2ca411d9e02644d2e15b38bc26c95e6"
        },
        type: 'post', //HTTP请求类型
        url:'https://5166000.com/api_digital/Order/getPcList' ,
        dataType: 'json',
        timeout: 10000, //超时时间设置为10秒；
        async: false,
		data:{
            categoryId: "",
            count: 10,
            isFollow: 0,
            lotteryId: "",
            nonce: "31b3ed1b-3ae8-4539-9c9a-02cb6baf7a52",
            page: 1,
            timestamp: 1537968104,
            type: "all"
		},
        success: function(data, status, xhr) {
            data.data.result.every(function (val, index) {
                console.info(val,val.id,val.issueNo,issueNo)
                if (issueNo===val.issueNo){//和要判断的期号相同
                    //获取详情查看当时买的市大还是小
                    console.info("相等的号码。。。")
                    getDetail(val.id,dx)
                    return false;
                }else{return true;}
            })
        },
        error: function(xhr, type, errorThrown) {
            console.info("异常："+errorThrown);
        }
    });
}

/**
 * 获取详情
 * @param id
 */
function getDetail(id,dx){
    $.ajax({
        headers: {
            "Auth-Identity": "normal",
            "Auth-Token": auth.value.token,
            "Sign": "c2ca411d9e02644d2e15b38bc26c95e6"
        },
        type: 'post', //HTTP请求类型
        url:'https://5166000.com/api_digital/Order/getDetail' ,
        dataType: 'json',
        timeout: 10000, //超时时间设置为10秒；
        async: false,
        data:{
            id: id,
            nonce: "bacda7da-bfcf-4383-8124-eb1ac12de3f4",
            timestamp: 1537968291,
        },
        success: function(data, status, xhr) {
            let betNumber = data.data.betNumber;
            console.info("订单详情",data,betNumber,dx)
            if (betNumber === dx) {
                console.info(data.data.issueNo,"  ","投注成功，立即去购买新的一期")
                let c = "大";
                if (dx==="大"){
                    c="小";
                }
                goumai(parseInt(data.data.issueNo)+1,c)
            }else {
                console.info(data.data.issueNo,"  ","投注失败，继续检测")
            }
        },
        error: function(xhr, type, errorThrown) {
            console.info("异常："+errorThrown);
        }
    });
}

